package com.atlin.project.service.impl;


import com.atlin.project.contant.CommonConstant;
import com.atlin.project.mapper.LoginInfoMapper;
import com.atlin.project.model.domain.LoginInfo;
import com.atlin.project.model.dto.loginInfo.LoginInfoQueryRequest;
import com.atlin.project.model.vo.LoginInfoVO;
import com.atlin.project.service.LoginInfoService;
import com.atlin.project.utils.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description 针对表【login_info(系统访问记录)】的数据库操作Service实现
 * @createDate 2024-05-25 14:31:21
 */
@Service
public class LoginInfoServiceImpl extends ServiceImpl<LoginInfoMapper, LoginInfo>
        implements LoginInfoService {

    @Override
    public QueryWrapper<LoginInfo> getQueryWrapper(LoginInfoQueryRequest loginInfoQueryRequest) {
        QueryWrapper<LoginInfo> queryWrapper = new QueryWrapper<>();
        if (loginInfoQueryRequest == null) {
            return queryWrapper;
        }
        Long id = loginInfoQueryRequest.getId();
        String userAccount = loginInfoQueryRequest.getUserAccount();
        String ipaddr = loginInfoQueryRequest.getIpaddr();
        String loginLocation = loginInfoQueryRequest.getLoginLocation();
        String browser = loginInfoQueryRequest.getBrowser();
        String os = loginInfoQueryRequest.getOs();
        String status = loginInfoQueryRequest.getStatus();
        String msg = loginInfoQueryRequest.getMsg();
        Date loginTime = loginInfoQueryRequest.getLoginTime();
        String sortField = loginInfoQueryRequest.getSortField();
        String sortOrder = loginInfoQueryRequest.getSortOrder();

        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.like(StringUtils.isNotBlank(userAccount), "userAccount", userAccount);
        queryWrapper.like(StringUtils.isNotBlank(ipaddr), "ipaddr", ipaddr);
        queryWrapper.like(StringUtils.isNotBlank(loginLocation), "loginLocation", loginLocation);
        queryWrapper.like(StringUtils.isNotBlank(browser), "browser", browser);
        queryWrapper.like(StringUtils.isNotBlank(os), "os", os);
        queryWrapper.eq(StringUtils.isNotBlank(status), "status", status);
        queryWrapper.like(StringUtils.isNotBlank(msg), "msg", msg);
        queryWrapper.eq(loginTime != null, "loginTime", loginTime);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        return queryWrapper;
    }

    @Override
    public LoginInfoVO getLoginInfoVO(LoginInfo loginInfo, HttpServletRequest request) {
        return LoginInfoVO.objToVo(loginInfo);
    }

    @Override
    public Page<LoginInfoVO> getLoginInfoVOPage(Page<LoginInfo> questionPage, HttpServletRequest request) {
        List<LoginInfo> loginInfoList = questionPage.getRecords();
        Page<LoginInfoVO> loginInfoVOPage = new Page<>(questionPage.getCurrent(), questionPage.getSize(), questionPage.getTotal());
        if (loginInfoList.isEmpty()) {
            return loginInfoVOPage;
        }

        List<LoginInfoVO> loginInfoVOList = loginInfoList.stream().map(LoginInfoVO::objToVo).collect(Collectors.toList());
        loginInfoVOPage.setRecords(loginInfoVOList);
        return loginInfoVOPage;
    }
}




